<!-- eslint-disable no-console -->
<!--------------------------------
 - @Author: Ronnie Zhang
 - @LastEditor: Ronnie Zhang
 - @LastEditTime: 2023/12/05 21:29:56
 - @Email: zclzone@outlook.com
 - Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
 --------------------------------->

<template>
  <CommonPage>
    <MeCrud ref="$table" v-model:query-items="queryItems" :scroll-x="1200" :columns="columns" :get-data="api.getPage">
      <MeQueryItem label="销售单" :label-width="50">
        <n-input v-model:value="queryItems.saleOrderNum" type="text" placeholder="请输入订单号" clearable />
      </MeQueryItem>
      <MeQueryItem label="配送单" :label-width="50">
        <n-input v-model:value="queryItems.deliveryOrderNum" type="text" placeholder="请输入订单号" clearable />
      </MeQueryItem>
    </MeCrud>
  </CommonPage>
</template>

<script setup>
import { MeCrud, MeQueryItem } from '@/components'
import { NButton } from 'naive-ui'
import api from './api'

defineOptions({ name: 'GoodsMgt' })
const router = useRouter()
const route = useRoute()
const $table = ref(null)
/** QueryBar筛选参数（可选） */
const queryItems = ref({})

onMounted(() => {
  if (route.query.saleOrderNum) {
    queryItems.value.saleOrderNum = route.query.saleOrderNum
  }
  if (route.query.deliveryOrderNum) {
    queryItems.value.deliveryOrderNum = route.query.deliveryOrderNum
  }
  $table.value?.handleSearch()
})

const columns = [
  {
    title: '销售单号',
    key: 'saleOrder.orderNum',
    width: 100,
    render: (rew) => {
      return h(
        NButton,
        { onClick: () => toDeliveryOrder(rew.orderNum) },
        { default: () => rew.saleOrder?.orderNum },
      )
    },
  },
  {
    title: '配送单号',
    key: 'deliveryOrder.deliveryOrderNum',
    width: 100,
    render: (rew) => {
      return h(
        NButton,
        { onClick: () => toDeliveryOrder(rew) },
        { default: () => rew.deliveryOrder.deliveryOrderNum },
      )
    },
  },
  // { title: '平台单号', key: 'errandSourceNum', width: 90 },
  { title: '运费', key: 'deliveryFee', width: 90 },
  { title: '距离', key: 'errandDistance', width: 90 },
  {
    title: '骑手姓名',
    key: 'transporterName',
    width: 100,
  },
  {
    title: '骑手电话',
    key: 'transporterName',
    width: 100,
  },
  {
    title: '状态',
    key: 'errandOrderStatusName',
    width: 100,
  },
  {
    title: '创建时间',
    key: 'createTime',
    width: 100,
  },
  {
    title: '操作',
    key: 'actions',
    width: 320,
    align: 'center',
    fixed: 'right',
    hideInExcel: true,
    render(row) {
      return [
        h(
          NButton,
          {
            size: 'small',
            type: 'primary',
            secondary: true,
            onClick: () => openErrandOrderDetailsModel(row),
          },
          {
            default: () => '详情',
            icon: () => h('i', { class: 'i-carbon:user-role text-14' }),
          },
        ),
      ]
    },
  },
]
function openErrandOrderDetailsModel(row) {
  router.push({ path: `/order/errand-order/${row.id}`, query: { orderNum: row.saleOrder.orderNum, orderId: row.saleOrder.id, errandId: row.id } })
}
//
function toDeliveryOrder(rew) {
  router.push({ path: `/order/delivery-order`, query: { deliveryOrderNum: rew.deliveryOrder.deliveryOrderNum } })
}
</script>
